package com.house.controller;

import com.github.pagehelper.PageInfo;
import com.house.entity.House;
import com.house.entity.Users;
import com.house.service.HouseService;
import com.house.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RestController

@RequestMapping("/house/")  //注册
@CrossOrigin(value = "*",allowCredentials ="true")
public class HouseController {
    private String savePath="E:\\images";
    @Autowired
    private  HouseService houseService;
    @RequestMapping("fabuHouse")
        public BaseResult fabuHouse(@RequestParam(value = "photo",required = false)
        MultipartFile file, House house, HttpSession session
        ){
   try {
       if(!file.isEmpty()) {
           String upload = FileUploadUtil.uploadFile(savePath, file);
           //设置出租房的图片路径
           System.out.println("文件名是:" + upload);
           house.setPath(upload);
           //设置出租房的编号(唯一)
       }
           house.setId(System.currentTimeMillis() + "");
            house.setIsdel(0);
            //设置当前的用户编号 (暂不考虑)??
    Users usersd = (Users)session.getAttribute("loginInfo");
       house.setUserId(usersd.getId());

            int i = this.houseService.addHouse(house);

            return new BaseResult(BaseResult.RESULT_SUCCESS,Contant.House.successMsg);
          } catch (Exception e) {
                e.printStackTrace();
            }
        return new BaseResult(BaseResult.RESULT_FAIL,Contant.House.failMsg);
    }



    @RequestMapping("getHouseByPage")   //分页显示
    public BaseResult getHouseByPage(PageParmeter pageParmeter, HttpSession session){
        Users usersd = (Users)session.getAttribute("loginInfo");

        PageInfo<House> houseByUser = this.houseService.getHouseByUser(1030, pageParmeter);

        return new BaseResult(BaseResult.RESULT_SUCCESS,houseByUser);
    }

    //删除出租房   传id
    @RequestMapping("delHouse")
    public BaseResult delHouse(String id){
        //调用业务获取分页信息  1表示删除
        int temp=this.houseService.deleteHouse(id,1);
        if (temp>0){
            return new BaseResult(BaseResult.RESULT_SUCCESS,Contant.House.delsuccessMsg);
        }
        return new BaseResult(BaseResult.RESULT_FAIL,Contant.House.delfailMsg);
    }


    @RequestMapping("getHouse")
      public BaseResult getHouse(String id){
        House house = this.houseService.gestHouse("1596442837339");
        return new BaseResult(BaseResult.RESULT_SUCCESS,house);
    }

    @RequestMapping ("searchHouse")
    public BaseResult searchHouse(HouseCondition houseCondition){
        PageInfo<House> broswerHouse = this.houseService.getBroswerHouse(houseCondition);
        return new BaseResult(BaseResult.RESULT_SUCCESS,broswerHouse);
    }

}



